package com.redsoft.epip.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.redsoft.epip.handler.MsgHandler;
import com.redsoft.epip.handler.NullHandler;
import com.redsoft.epip.handler.ScanHandler;
import com.redsoft.epip.handler.SubscribeHandler;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;

/**
 * wechat mp configuration
 *
 * @author Binary Wang(https://github.com/binarywang)
 */
@Configuration
@ConditionalOnClass(WxMpService.class)
@EnableConfigurationProperties(WechatMpProperties.class)
public class WechatMpConfiguration {
	// @Autowired
	// protected LogHandler logHandler;
	 @Autowired
	 protected NullHandler nullHandler;
	// @Autowired
	// protected KfSessionHandler kfSessionHandler;
	// @Autowired
	// protected StoreCheckNotifyHandler storeCheckNotifyHandler;
	@Autowired
	private WechatMpProperties properties;
	// @Autowired
	// private LocationHandler locationHandler;
	// @Autowired
	// private MenuHandler menuHandler;
	 @Autowired
	 private MsgHandler msgHandler;
//	@Autowired
//    private UnsubscribeHandler unsubscribeHandler;
    @Autowired
    private SubscribeHandler subscribeHandler;
    
//    @Autowired
//    private WxCardHandler wxCardHandler;
	
	@Autowired
	private ScanHandler scanHandler;
	 
	@Bean
	@ConditionalOnMissingBean
	public WxMpConfigStorage configStorage() {
		WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
		configStorage.setAppId(this.properties.getAppId());
		configStorage.setSecret(this.properties.getSecret());
		configStorage.setToken(this.properties.getToken());
		configStorage.setAesKey(this.properties.getAesKey());
		return configStorage;
	}

	@Bean
	@ConditionalOnMissingBean
	public WxMpService wxMpService(WxMpConfigStorage configStorage) {
		// WxMpService wxMpService = new
		// me.chanjar.weixin.mp.api.impl.okhttp.WxMpServiceImpl();
		// WxMpService wxMpService = new
		// me.chanjar.weixin.mp.api.impl.jodd.WxMpServiceImpl();
		// WxMpService wxMpService = new
		// me.chanjar.weixin.mp.api.impl.apache.WxMpServiceImpl();
		WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(configStorage);
		return wxMpService;
	}

	@Bean
	public WxMpMessageRouter router(WxMpService wxMpService) {
		final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);

		// // 记录所有事件的日志 （异步执行）
		// newRouter.rule().handler(this.logHandler).next();
		//
		// // 接收客服会话管理事件
		// newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		// .event(WxMpEventConstants.CustomerService.KF_CREATE_SESSION)
		// .handler(this.kfSessionHandler).end();
		// newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		// .event(WxMpEventConstants.CustomerService.KF_CLOSE_SESSION)
		// .handler(this.kfSessionHandler)
		// .end();
		// newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		// .event(WxMpEventConstants.CustomerService.KF_SWITCH_SESSION)
		// .handler(this.kfSessionHandler).end();
		//
		// // 门店审核事件
		// newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		// .event(WxMpEventConstants.POI_CHECK_NOTIFY)
		// .handler(this.storeCheckNotifyHandler).end();
		//
		
		 // 用户发送消息事件
		 newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		 .event(WxConsts.CUSTOM_MSG_TEXT).handler(this.msgHandler).end();
		
		 // 自定义菜单事件
		 newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		 .event(WxConsts.BUTTON_CLICK).handler(this.nullHandler).end();
		
//		 // 点击菜单连接事件
//		 newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
//		 .event(WxConsts.BUTTON_VIEW).handler(this.nullHandler).end();
		
		 // 关注事件
		 newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		 .event(WxConsts.EVT_SUBSCRIBE).handler(this.getSubscribeHandler())
		 .end();
//		 //微信卡券领取事件推送
//		 newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
//		 .event(WeChatConsts.EVENT_USER_GET_CARD).handler(this.getWxCardHandler())
//		 .end();
		//
		// // 取消关注事件
		// newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		// .event(WxConsts.EVT_UNSUBSCRIBE)
		// .handler(this.getUnsubscribeHandler()).end();
		//
		// // 上报地理位置事件
		// newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		// .event(WxConsts.EVT_LOCATION).handler(this.getLocationHandler())
		// .end();
		//
		// // 接收地理位置消息
		// newRouter.rule().async(false).msgType(WxConsts.XML_MSG_LOCATION)
		// .handler(this.getLocationHandler()).end();
		//
		 // 扫码事件
		 newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
		 .event(WxConsts.EVT_SCAN).handler(this.getScanHandler()).end();
		//
//		 // 默认
//		 newRouter.rule().async(false).handler(this.msgHandler).end();

		return newRouter;
	}

	public ScanHandler getScanHandler() {
		return scanHandler;
	}
	
	// protected MenuHandler getMenuHandler() {
	// return this.menuHandler;
	// }

//	 public WxCardHandler getWxCardHandler() {
//		return wxCardHandler;
//	}

	protected WxMpMessageHandler getSubscribeHandler() {
		 return this.subscribeHandler;
	 }
	//
	// protected UnsubscribeHandler getUnsubscribeHandler() {
	// 		return this.unsubscribeHandler;
	// }
	//
	// protected AbstractHandler getLocationHandler() {
	// return this.locationHandler;
	// }
	//
	// protected MsgHandler getMsgHandler() {
	// return this.msgHandler;
	// }
	//
	// protected AbstractHandler getScanHandler() {
	// return null;
	// }

}